# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=ocaml5-loongarch
pkgver=5.2.0
pkgrel=0
_gittag="$pkgver-la"
pkgdesc="Loongarch port of the OCaml programming language"
url="https://github.com/loongson/ocaml"
arch="loongarch64"
provides="ocaml5=$pkgver-r$pkgrel"
license="LGPL-2.1-or-later WITH OCaml-LGPL-linking-exception"
depends="$pkgname-runtime=$pkgver-r$pkgrel gcc"
checkdepends="parallel"
subpackages="
	$pkgname-doc
	$pkgname-ocamldoc
	$pkgname-compiler-libs:_compiler_libs
	$pkgname-runtime
	"
source="https://github.com/loongson/ocaml/archive/refs/tags/v$_gittag/ocaml5-$_gittag.tar.gz"
builddir="$srcdir/ocaml-$_gittag"
options="textrels" # found in str.cmxs, runtime_events.cmxs, unix.cmxs

build() {
	want_check && extra_conf_opts="--enable-ocamltest"

	./configure \
		--prefix /usr \
		--bindir /usr/bin \
		--libdir /usr/lib/ocaml \
		--mandir /usr/share/man \
		--docdir /usr/share/doc/ocaml5 \
		--enable-native-compiler \
		--enable-native-toplevel \
		--disable-installing-bytecode-programs \
		--disable-installing-source-artifacts \
		$extra_conf_opts \
		CC="${CC:-gcc}" \
		AS="${CC:-gcc} -c" \
		ASPP="${CC:-gcc} -c"
	make world.opt
}

check() {
	make ocamltest
	make -C testsuite parallel
}

package() {
	make DESTDIR="$pkgdir" install
}

doc() {
	provides="ocaml5-doc=$pkgver-r$pkgrel"
	default_doc
}

ocamldoc() {
	pkgdesc="$pkgdesc (documentation generator)"
	depends="$pkgname=$pkgver-r$pkgrel"
	provides="ocaml5-ocamldoc=$pkgver-r$pkgrel"

	amove usr/bin/ocamldoc*
	amove usr/lib/ocaml/ocamldoc
}

_compiler_libs() {
	pkgdesc="$pkgdesc (compiler libraries)"
	depends="$pkgname=$pkgver-r$pkgrel"
	provides="
		ocaml5-compiler-libs=$pkgver-r$pkgrel
		ocaml5-dev=$pkgver-r$pkgrel
		"

	amove usr/lib/ocaml/compiler-libs
}

runtime() {
	pkgdesc="$pkgdesc (runtime environment)"
	depends=
	provides="ocaml5-runtime=$pkgver-r$pkgrel"

	amove usr/bin/ocamlrun*
	amove usr/lib/ocaml/ld.conf

	local i; for i in \
		'*.cmo' \
		'*.cmi' \
		'*.cma' \
		'stublibs' \
		'threads/*.cmi' \
		'threads/*.cma'
	do
		amove "usr/lib/ocaml/$i"
	done
}

sha512sums="
9854668b75577ef805a2383fdd1bb283a808a6858c48517c201f7feb43573d4eaf31aee2acfe44b66f470458d8402a434a0e3f7fc10a3b0e5a80a86e5a700823  ocaml5-5.2.0-la.tar.gz
"
